cc.Class({
    extends: cc.Component,

    properties: {
        basketball:cc.PhysicsCircleCollider,
        gameView:null,
        isCheckQiuKuang:false,
        posX:0,
        posY:0,
    },

    // 只在两个碰撞体开始接触时被调用一次
    onBeginContact: function (contact, selfCollider, otherCollider) {
        if(this.gameView == null)
        {
            this.gameView = selfCollider.node.parent.getComponent("gameMain");
        }

        if(selfCollider.tag != 3 && otherCollider.tag != 3)
        {
            if(window.gameType == "play")
            {
                window.audioMgr.prototype.playQiuColliderAudio();
            }
        }
       
        //是否点击弹杆引起的碰撞
        if(this.gameView.isCollider)
        {
            if(selfCollider.node.name == "tanGang")
            {
                this.setBasketballSpeed(otherCollider);
            }
            else if(otherCollider.node.name == "tanGang")
            {
                this.setBasketballSpeed(selfCollider);
            }

            this.gameView.isCollider = false;
        }
       
        //篮球是否与篮筐碰撞
        if(selfCollider.tag == 7 || otherCollider.tag == 7)
        {
            //保存篮球的x与y坐标
            if(selfCollider.node.name == "qiuKuang")
            {
                this.posX = otherCollider.node.x;
                this.posY = otherCollider.node.y;
            }
            else
            {
                this.posX = selfCollider.node.x;
                this.posY = selfCollider.node.y;
            }
            this.isCheckQiuKuang = true;
        }
    },

    // 只在两个碰撞体结束接触时被调用一次
    onEndContact: function (contact, selfCollider, otherCollider) {
        //是否与球网处的碰撞体接触                             //检测是否中球
        if(selfCollider.tag == 8 || otherCollider.tag == 8 )// if(this.isCheckQiuKuang)
        {
            //保存篮球的x与y坐标
            if(selfCollider.node.name == "qiuKuang")
            {
                this.isPassLevel(otherCollider.node);
            }
            else
            {
                this.isPassLevel(selfCollider.node);
            }
        }
        //检测篮球是否出局
        else if(otherCollider.node.name == "di")
        {
            //tag 3 表示篮球接触到底部  游戏失败
            if(otherCollider.tag == 3)
            {
                //进入游戏失败函数
                if(window.gameType == "win")return;
                window.audioMgr.prototype.playGameLoseAudio();
                window.gameType = "lose";
                this.scheduleOnce(()=>{
                    this.gameView.onGameOver();
                },3);
            }
        }
    },


    setBasketballSpeed:function(ball,type){
        //改变球体的线性速度  向量除法  给球一个加速
        // ball.restitution = 1;
        ball.body.linearVelocity = ball.body.linearVelocity.div(0.1);
        this.gameView.isCollider = false;
    },

    //是否中球
    isPassLevel:function(data){
        if(this.isCheckQiuKuang)
        {
            this.gameView.showPassQiuAnim();
            this.isCheckQiuKuang = false;
            if(this.posY - data.y > 95)
            {
                this.gameView.inTheBall();
            }
            else
            {
            }
        }
    },
});
